<template>
  <div>
    <Title title="班级列表"/>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="Id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级口令">
        <template slot-scope="scope">
          <span>{{ scope.row.command_word }}</span>
        </template>
      </el-table-column>

      <el-table-column label="班级人数">
        <template slot-scope="scope">
          <span>{{ scope.row.count }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入班级名搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page"
      background
      layout="prev, pager, next"
      :total="total"
      @prev-click="getData"
      @next-click="getData"
      @current-change="getData"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getAllClass, delClass } from "@/api/class";
import { formatTime, error, success } from "@/utils";
import Title from '@/components/common/Title'
export default {
  data() {
    return {
      total: 0,
      tableData: [],
      pageNum: 0,
      search: "",
    };
  },
  components: {
    Title
  },
  async created() {
    this.getData();
  },
  methods: {
    /**
     * 编辑班级
     */
    handleEdit(_, row) {
      this.$store.commit("setActiveIndex", "3-2");
      this.$router.push({ path: "/createClass", query: { id: row.id } });
    },
    /**
     * 删除的班级
     */
    async handleDelete(_, row) {
      this.$confirm("此操作将永久删除该班级,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delClass(row.id);
          if (res.code === 1) {
              // 重新获取数据
            this.getData(this.pageNum);
            success(res.msg);
          } else {
            error(res.msg);
          }
        })
        .catch(() => {
          info("已取消删除");
        });
    },
    /**
     * 获取数据
     */
    async getData(pageNum = 1, pageSize = 10) {
      this.pageNum = pageNum;
      const res = await getAllClass(pageNum, pageSize);
      if (res.code === 1) {
        this.tableData = formatTime(res.data);
        if (this.total === 0) {
          this.total = res.data[0]?.total || 0;
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  margin-top: 20px;
}
</style>
